<template>
    <div class="goodslist">
        <!-- a标签跳转跳转 -->
        <!-- <router-link :to="'/home/goodslist/'+item.id" class="goodsitem" tag="div" v-for='item in list' :key="item.id">
            <img :src="item.src" alt="">
            <h2>{{ item.name }}</h2>
            <div class="info">
                <p class="price">
                    <span class="now">{{ '￥' + item.sell }}</span>
                    <span class="old">{{ '￥' + item.price}}</span>
                </p>
                <div class="sell">
                <span>热卖中</span>
                <span>剩余60件</span>
            </div>
            </div>
        </router-link> -->

        <!-- 编程式导航 -->
        <div :to="'/home/goodslist/'+item.id" class="goodsitem" tag="div" v-for='item in list' :key="item.id" @click="goDetail(item.id)">
            <img :src="item.src" alt="">
            <h2>{{ item.name }}</h2>
            <div class="info">
                <p class="price">
                    <span class="now">{{ '￥' + item.sell }}</span>
                    <span class="old">{{ '￥' + item.price}}</span>
                </p>
                <div class="sell">
                <span>热卖中</span>
                <span>剩余60件</span>
            </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data(){
        return {
            list:[
                {
                    name: '小米',
                    price: 999 ,
                    sell: 899,
                    id: 0,
                    src: './components/photos/0/0.png'
                },
                {
                    name: '华为',
                    price: 2999 ,
                    sell: 2899,
                    id: 1,
                    src: './components/photos/1/1.png'
                },
                {
                    name: '苹果',
                    price: 6999 ,
                    sell: 6899,
                    id: 2,
                    src: './components/photos/2/2.png'
                },
            ],
            index: 0,
        }
    },
    created(){
        localStorage.setItem('goodslist',JSON.stringify(this.list))
    },
    methods:{
        change(id){
            this.index = id
        },
        goDetail(id){
            //使用js的形式进行路由导航，即用  router.push()
            //this.$route 是路由参数对象，所有跟路由中的参数存储于其中
            //this.$router 是路由导航对象，用它可以方便地使用js代码实现路由的前进后退跳转到新的url地址

            //基本款
            // this.$router.push('/home/goodslist/'+id)   

            //对象
            // this.$router.push({path:'/home/goodslist/'+id})

            //命名
            this.$router.push({name:'goodsinfo', params: { id }})
        }
    }
}
</script>

<style scoped>
.goodslist{
    display: flex;
    flex-wrap: wrap;
}
.goodsitem{
    width: 43.5%;
    overflow: hidden;
    border: 1px solid #ddd;
    box-shadow: 0 0 8px #ccc;
    margin: 5px;
    margin-left: 3%;
    cursor: pointer;
}
h2{
    font-size: 14px;
}

.goodsitem img {
    display: inline-block;
    width: 100%;
    padding: 3px;
}
.info{
    background-color: #eee;
}
.now{
    font-weight: 700;
    color: red;
    font-size: 23px;
}
.old{
    text-decoration: line-through;
    margin-left: 10px;
}
.sell{
    display: flex;
    justify-content: space-between ;
    font-size: 12px;
}
</style>